package com.fs.controller;

import com.fs.global.config.CustomConfig;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Arrays;

@Component
public class PicFitUtil {
    private final CustomConfig customConfig;
    private final String SHOP_SAVE_PIC_DIR = "shop";
    private final String ROOM_SAVE_PIC_DIR = "room";

    public PicFitUtil(CustomConfig customConfig) {
        this.customConfig = customConfig;
    }

    public String getAbsImageDirOfRoom(Integer roomId) {
        return getImageSaveDir(ROOM_SAVE_PIC_DIR, roomId);
    }

    public String getAbsImageDirOfShop(Integer shopId) {
        return getImageSaveDir(SHOP_SAVE_PIC_DIR, shopId);
    }

    public String getRelImagePathOfShop(String filename, Integer shopId) {
        return String.join(File.separator, Arrays.asList(SHOP_SAVE_PIC_DIR, shopId.toString(), filename));
    }

    public String getRelImagePathOfRoom(String filename, Integer roomId) {
        return String.join(File.separator, Arrays.asList(ROOM_SAVE_PIC_DIR, roomId.toString(), filename));
    }

    private String getImageSaveDir(String relPath, Integer resourceId) {
        String fileSaveDir = String.join(File.separator, Arrays.asList(customConfig.getPicfitDir(), relPath, resourceId.toString()));
        File dir = new File(fileSaveDir);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                throw new RuntimeException("创建图片文件夹失败");
            }
        }
        return fileSaveDir;
    }

    public String getShopSavePicDir() {
        return SHOP_SAVE_PIC_DIR;
    }

    public String getRoomSavePicDir() {
        return ROOM_SAVE_PIC_DIR;
    }
}
